﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MvcApplication4.Models;
using Syncfusion.DocIO.DLS;
using Syncfusion.Mvc.Grid;
using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Membership = System.Web.Security.Membership;

namespace MvcApplication4.Controllers
{
    public class CadastroFuncionarioController : Controller
    {
        private BD_Vacinas2Context db = new BD_Vacinas2Context();

        //
        // GET: /CadastroFuncionario/

        public ActionResult Index()
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Funcionario.Take(100).ToList();
            return View(data);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(PagingParams args)
        {
            IEnumerable data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Funcionario.Take(100).ToList();
            return data.GridActions<tb_Funcionario>();
        }

        public ActionResult ExportToExcel()
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Funcionario.Take(200).ToList();
            return data.GridExportToExcel<tb_Funcionario>("GridExcel.xlsx", ExcelVersion.Excel2010);
        }

        public ActionResult ExportToWord(PagingParams args)
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Funcionario.Take(200).ToList();
            return data.GridExportToWord<tb_Funcionario>("GridWord.doc", DocumentVersion.Word2007, args.ExportOption);
        }

        public ActionResult ExportToPDF(PagingParams args)
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Funcionario.Take(200).ToList();
            return data.GridExportToPDF<tb_Funcionario>("GridPDF.pdf", PdfVersion.Version1_7, args.ExportOption);
        }

        //
        // GET: /CadastroFuncionario/Details/5

        public ActionResult Details(int id = 0)
        {
            tb_Funcionario tb_funcionario = db.tb_Funcionario.Find(id);
            if (tb_funcionario == null)
            {
                return HttpNotFound();
            }
            return View(tb_funcionario);
        }

        //
        // GET: /CadastroFuncionario/Create

        public ActionResult Create()
        {
            ViewBag.ID_UBS = new SelectList(db.tb_UBS, "ID_UBS", "Nome_UBS");
            return View();
        }

        [HttpPost]
        public JsonResult doesUserNameExist(string UserName, tb_Funcionario tb_funcionario)
        {

            var user = Membership.GetUser(tb_funcionario.Login_Fun);

            return Json(user == null);
        }

        [HttpPost]
        public JsonResult doesEmailNameExist(tb_Funcionario tb_funcionario)
        {

            var func = from f in db.tb_Funcionario
                       select f;

            func = func.Where(o => o.Email.Equals(tb_funcionario.Email));

            return Json(!func.Any());
        }

        [HttpPost]
        public JsonResult doesCPFExist(tb_Funcionario tb_funcionario)
        {

            var func = from f in db.tb_Funcionario
                       select f;

            func = func.Where(o => o.CPF.Equals(tb_funcionario.CPF));

            return Json(!func.Any());
        }

        //
        // POST: /CadastroFuncionario/Create

        [HttpPost]
        public ActionResult Create(tb_Funcionario tb_funcionario)
        {
            if (ModelState.IsValid)
            {
                db.tb_Funcionario.Add(tb_funcionario);
                Membership.CreateUser(tb_funcionario.Login_Fun, tb_funcionario.Senha);
                Roles.AddUserToRole(tb_funcionario.Login_Fun, "USUARIO");
                MembershipUser user = Membership.GetUser(tb_funcionario.Login_Fun);
                user.Email = tb_funcionario.Email;  
                Membership.UpdateUser(user);
                db.SaveChanges();
                return RedirectToAction("Index");    
            }

            ViewBag.ID_UBS = new SelectList(db.tb_UBS, "ID_UBS", "Nome_UBS", tb_funcionario.ID_UBS);
            return View(tb_funcionario);
        }

        //
        // GET: /CadastroFuncionario/Edit/5

        public ActionResult Edit(int id = 0)
        {
            tb_Funcionario tb_funcionario = db.tb_Funcionario.Find(id);
            if (tb_funcionario == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_UBS = new SelectList(db.tb_UBS, "ID_UBS", "Nome_UBS", tb_funcionario.ID_UBS);
            return View(tb_funcionario);
        }

        //
        // POST: /CadastroFuncionario/Edit/5

        [HttpPost]
        public ActionResult Edit(tb_Funcionario tb_funcionario)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_funcionario).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ID_UBS = new SelectList(db.tb_UBS, "ID_UBS", "Nome_UBS", tb_funcionario.ID_UBS);
            return View(tb_funcionario);
        }

        //
        // GET: /CadastroFuncionario/Delete/5

        public ActionResult Delete(int id = 0)
        {
            tb_Funcionario tb_funcionario = db.tb_Funcionario.Find(id);
            if (tb_funcionario == null)
            {
                return HttpNotFound();
            }
            return View(tb_funcionario);
        }

        //
        // POST: /CadastroFuncionario/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            tb_Funcionario tb_funcionario = db.tb_Funcionario.Find(id);
            db.tb_Funcionario.Remove(tb_funcionario);
            Membership.DeleteUser(tb_funcionario.Login_Fun);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

}